<?php
	namespace Admin\Controller;
	use Admin\Controller\AdminController;
	class MenuController extends AdminController{
		/**
		*查询菜单结果集
		*/
		public function index(){
			$pid=I('get.pid',0);
			$title=trim(I('get.title'));
			if($title)
				$map['title'] = array('like',"%{$title}%");
  
			if($pid){
				$data=M('Menu')->where("id={$pid}")->field(true)->find();

				$this->assign("data",$data);
			}
			
			$all_menu=M('Menu')->getField('id,title');

			$map['pid']=$pid;
			$list=$this->lists('Menu',$map);
			//echo "<pre/>";print_r($list);
			int_to_string($list,array('hide'=>array(1=>'是',0=>'否'),'is_dev'=>array(1=>'是',0=>'否')));
			if($list){
				foreach($list as &$value){
					if($value['pid']){
						$value['up_title']=$all_menu[$value['pid']];
					}
				}
			}
			//echo "<pre/>";print_r($list);
			$this->assign("list",$list);

			// 记录当前列表页的cookie
			Cookie('__forward__',$_SERVER['REQUEST_URI']);
			$this->meta_title = '菜单列表';
			$this->display();
		}
		/**
		 * 新增菜单
		 */
		public function add(){
			// echo I('get.pid',0);
			if(IS_POST){
				$addmenu=D('Menu');
				if($addmenu->create()){
					if($addmenu->add()){
						$this->success('新增成功',Cookie('__forward__'));
					}else{
						$this->error("新增失败");
					}
				}else{
					$this->error($addmenu->getError());
				}
			}
			else{

				 $this->assign("info",array('pid'=>I('get.pid')));
				 $menus=D('Menu')->field(true)->select();
				 
				 $menus =D('Common/Tree')->toFormatTree($menus);
				 $menus = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')), $menus);
				 //echo "<pre/>";print_r($menus);
				 $this->meta_title = '新增菜单';
				 $this->assign('Menus', $menus);
				 $this->display('edit');

			}

	    }
		  /**
		 * 编辑配置
		 */
		public function edit($id =0){
			 if(IS_POST){
				 $addmenu=D('Menu');
				if($addmenu->create()){
					if($addmenu->save()!==false){
						$this->success('编辑成功',Cookie('__forward__'));
					}else{
						$this->error("编辑失败");
					}
				}else{
					$this->error($addmenu->getError());
				}

			 }
			 else{
				 $info=array();
				 $id=I('get.id');
				 $info= M('Menu')->field(true)->find($id);
				 if($info==false){
					 $this->error("获取后台菜单信息错误");
				 }
				 $this->assign("info",$info);
				 $menus=M('Menu')->field(true)->select();
				 $menus=D('Common/Tree')->toFormatTree($menus);
				 $menus=array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')),$menus);
			     $this->assign('Menus', $menus);

			     $this->meta_title = '编辑后台菜单';
				 $this->display('edit');
			 }
		}
		 /**
		 * 删除后台菜单
		 */
		public function del(){
			$id = array_unique((array)I('get.id',0));//移除数组中重复的值
			if(empty($id)){
				$this->error('请选择要操作的数据!');
			}
			/*
			$map=array('id'=>array('in',$id));
			if(M('Menu')->where($map)->delete()){
               $this->success('删除成功');
			} else {
				$this->error('删除失败！');
			}
			*/
			
				
			//$map=array('id'=>$id);
			$map=array('id'=>array('in',$id));
			$model=M('Menu');
			
			case_del($model,$map);
			
		 }
		 public function toogleDev($id ,$value= 1){
			 $this->editRow('Menu', array('is_dev'=>$value), array('id'=>$id));
		 }
		 public function toogleHide($id ,$value= 1){
			 $this->editRow('Menu', array('hide'=>$value), array('id'=>$id));
		 }
	}
?>